<template>
  <div>
    <!-- <p>文章列表</p> -->

    <!-- 头部 -->
    <el-card>
      <Bread style="margin-bottom: 40px" classify="文章列表"></Bread>
      <!-- 筛选 -->
      <div class="form-box">
        <el-form
          :inline="true"
          :model="q"
          ref="searchRef"
          class="demo-form-inline"
        >
          <el-form-item label="文章分类">
            <el-select v-model="q.cate_id" placeholder="请选择分类">
              <el-option
                v-for="item in cateList"
                :key="item.id"
                :label="item.cate_name"
                :value="item.id"
              ></el-option>
              <!-- <el-option label="科学" value="科学"></el-option> --> </el-select
            >===={{ q.cate_id }}
          </el-form-item>
          <el-form-item label="发布状态">
            <el-select v-model="q.state" placeholder="请选择状态">
              <el-option label="已发布" value="已发布"></el-option>
              <el-option label="草稿" value="草稿"></el-option> </el-select
            >=={{ q.state }}
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSbmit">筛选</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="pubArt">发布文章</el-button>
      </div>
      <!-- 筛选结束 -->
      <!-- 表单开始 -->
      <el-table :data="artList" style="width: 100%" border>
        <el-table-column label="文章标题" width="180" type="index">
          <template slot-scope="{ row }">
            <!-- <pre>{{ row }}</pre> -->
            <el-link type="primary" @click="showInfo(row.id)">{{
              row.title
            }}</el-link>
            <!-- 文章详情对话框 -->
            <!-- 文章详情的对话框 -->
            <el-dialog title="文章预览" :visible.sync="infoVisible" width="80%">
              <ArtInfo :articleInfo="artInfo"></ArtInfo>
              <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="infoVisible = false"
                  >关 闭</el-button
                >
              </span>
            </el-dialog>
            <!-- 文章详情对话框结束 -->
          </template>
        </el-table-column>
        <el-table-column label="分类" prop="cate_name"> </el-table-column>
        <el-table-column label="发布时间">
          <!-- 通过过滤器,显示用户看的懂的时间,全局定义 -->
          <template slot-scope="{ row }">{{ row.pub_date | timer }}</template>
        </el-table-column>
        <el-table-column label="状态" prop="state"> </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button size="mini" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- @click="handleDelete(scope.row.id)" -->
      <!-- 表单结束 -->
      <!-- 尾部页面跳转 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="q.pagenum"
          :page-sizes="[2, 3, 5, 10]"
          :page-size="q.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 尾部页面跳转结束 -->
      <!-- 发布文章 -->
      <el-dialog fullscreen title="收货地址" :visible.sync="dialogFormVisible">
        <Artpub @changFs="fn"></Artpub>
        <!-- <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false"
            >确 定</el-button
          >
        </div> -->
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
// 异步请求数据
// 6筛选功能,首先我们把之前获取的数据通过vuex拿过来,然后遍历,点击筛选,出现对应的数据,然后点击控制则为空
import ArtInfo from '../../components/ArtInfo.vue'
import Artpub from '../../components/Artpub.vue'
import { mapState } from 'vuex'
import { reqGetlistArticle, reqGetArtInfo } from '../../api/art.js'
export default {
  data () {
    return {
      dialogFormVisible: false,
      infoVisible: false,
      q: {
        pagenum: 1,
        pagesize: 1,
        cate_id: '',
        state: ''
      },
      artList: [],
      total: 0,
      // 文章详情对象
      artInfo: {}
    }
  },
  components: {
    ArtInfo,
    Artpub
  },
  // 初始化
  created () {
    this.initArticle()
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  methods: {
    // 筛选
    onSbmit () {
      this.q.pagenum = 1
      this.initArticle()
    },
    // 重置
    reset () {
      this.q.pagenum = 1
      this.q.cate_id = ''
      this.q.state = ''
      this.initArticle()
    },
    // 初始化表格
    async initArticle () {
      const { data: res } = await reqGetlistArticle(this.q)
      console.log(res, 888)
      // 然后把数据给 artList: [],
      if (res.code === 0) {
        // 然后把数据渲染到页面
        this.artList = res.data
      }
    },
    handleSizeChange (val) {
      this.q.pagesize = val
      this.initArticle()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.q.pagenum = val
      this.initArticle()
    },
    // 文章详情
    async showInfo (id) {
      console.log(id)
      const { data } = await reqGetArtInfo(id)
      if (data.code !== 0) return this.$message.error('文章预览失败！')
      this.artInfo = data.data
      this.infoVisible = true
    },
    pubArt () {
      this.dialogFormVisible = true
    },
    fn () {
      this.dialogFormVisible = false
      this.initArticle()
    }
  }
}
</script>

<style lang="less" scoped>
.form-box {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}
</style>
